/**
 * Created by Bin_Shi on 2016/11/17.
 * 地图数据相关脚本
 */

var map = {
    width:2000,
    height:2000
};
var obstacles = [
    {x0:400, y0:400, width:30},
    {x0:800, y0:400, width:20},
    {x0:1200, y0:400, width:15},
    {x0:1600, y0:400, width:25},
    {x0:400, y0:800, width:40},
    {x0:800, y0:800, width:15},
    {x0:1200, y0:800, width:25},
    {x0:1600, y0:800, width:30},
    {x0:400, y0:1200, width:20},
    {x0:800, y0:1200, width:20},
    {x0:1200, y0:1200, width:40},
    {x0:1600, y0:1200, width:15},
    {x0:400, y0:1600, width:25},
    {x0:800, y0:1600, width:35},
    {x0:1200, y0:1600, width:15},
    {x0:1600, y0:1600, width:30}
];

//获取当前视界的范围
function getSight(player, canvas){
    var sight = {
        x0:500,
        x1:500,
        y0:1500,
        y1:1500
    };
    if(!player){
        return sight;
    }
    var width = Math.floor(canvas.width / 2);
    var height = Math.floor(canvas.height / 2);
    if(map.width - player.x < width){
        sight.x0 = map.width - canvas.width;
        sight.x1 = map.width;
    }
    else if(player.x < width){
        sight.x0 = 0;
        sight.x1 = canvas.width;
    }
    else{
        sight.x0 = player.x - width;
        sight.x1 = player.x + width;
    }
    if(map.height - player.y < height){
        sight.y0 = map.height - canvas.height;
        sight.y1 = map.height;
    }
    else if(player.y < height){
        sight.y0 = 0;
        sight.y1 = canvas.height;
    }
    else{
        sight.y0 = player.y - height;
        sight.y1 = player.y + height;
    }
    return sight;
}

//重绘地图
function drawMap(canvas){
    var ctx = canvas.getContext("2d");
    //清空画布
    ctx.clearRect(0,0,canvas.width,canvas.height);
    //获取当前视界范围
    var sight = getSight(player, canvas);
    //绘制障碍物
    for(var x in obstacles){
        drawObstacles(obstacles[x], sight, ctx);
    }
    //根据视界来绘制边界障碍物
    ctx.fillStyle = "orange";
    //1.左边
    if(sight.x0 < border_width){
        if(sight.x0 == 0){
            ctx.fillRect(sight.x0, 0, border_width, canvas.height);
        }
        else{
            ctx.fillRect(0, 0, border_width - sight.x0, canvas.height);
        }
    }
    //2.右边
    if(sight.x1 > map.width - border_width){
        if(sight.x1 == map.width){
            ctx.fillRect(canvas.width - border_width, 0, border_width, canvas.height);
        }
        else{
            ctx.fillRect(canvas.width - border_width + map.width - sight.x1, 0, border_width - map.width + sight.x1, canvas.height);
        }

    }
    //3.上边
    if(sight.y0 < border_width){
        if(sight.y0 == 0){
            ctx.fillRect(0, sight.y0, canvas.width, border_width);
        }
        else{
            ctx.fillRect(0, 0, canvas.width, border_width - sight.y0);
        }

    }
    //4.下边
    if(sight.y1 > map.height - border_width){
        if(sight.y1 == map.height){
            ctx.fillRect(0, canvas.height - border_width, canvas.width, border_width);
        }
        else{
            ctx.fillRect(0, canvas.height - border_width + map.height - sight.y1, canvas.width, border_width - map.height + sight.y1);
        }

    }
}

function drawObstacles(ob, sight, ctx){
    var o_x0 = ob.x0;
    var o_x1 = o_x0 + ob.width;
    var o_y0 = ob.y0;
    var o_y1 = o_y0 + ob.width;
    if(o_x0 > sight.x1 || o_y0 > sight.y1 || o_x1 <= sight.x0 || o_y1 <= sight.y0){
        return false;
    }
    else{
        var x0 = o_x0 - sight.x0;
        var y0 = o_y0 - sight.y0;
        ctx.fillStyle = "orange";
        ctx.fillRect(x0, y0, ob.width, ob.width);
    }
    return true;
}